<template>
  <div>
    <!-- close :所有的、任意形式的-->
    <el-dialog
      :title="isAdd ? '添加管理员' : '修改管理员'"
      :visible.sync="info.isShow"
      width="45%"
      @close="cancel"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="上级分类" prop="pid">
          <el-select v-model="ruleForm.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="(item, index) in cateList"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="catename">
          <el-input v-model="ruleForm.catename"></el-input>
        </el-form-item>
        <el-form-item label="分类图片" prop="img">
          <!-- action ：上传文件的地址 -->
          <!-- list-type ：文件列表的类型，有文字类型的，有照片墙的 -->
          <!-- on-remove:移除图片时调用的函数 -->
          <!-- on-change:文件状态改变时的钩子函数 -->
          <!-- file-list:已上传的文件列表 -->
          <!-- on-exceed:超出限制时的钩子 -->
          <!-- on-preview:预览时的钩子 -->
          <el-upload
            action="#"
            list-type="picture-card"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
            :limit="1"
            :on-exceed="limitImg"
            :on-preview="handlePictureCardPreview"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="imageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入负责添加角色的函数
import { getCatelistTree, cateEdit, cateAdd } from "../../request";

let defaultData = {
  pid: 0,
  catename: "",
  status: 1,
  img: "",
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
  data() {
    return {
      fileList: [],
      imageUrl: "",
      dialogVisible: false,
      // 下拉菜单的数据
      cateList: [],
      // 表单的数据
      ruleForm: {
        pid: 0,
        catename: "",
        status: 1,
        img: "",
      },
      // 表单的规则
      rules: {
        pid: [{ required: true, message: "请选择上级分类", trigger: "blur" }],
        catename: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      defaultProps: {
        // 子节点
        children: "children",
        // 文字内容
        label: "title",
      },
      // 弹窗类型，添加还是修改
      isAdd: true,
    };
  },

  created() {
    // 获取下拉列表的数据
    this.getList();
  },
  // mounted() {
  //   // 设置树形控件中被选中的节点
  //   this.setKeys();
  // },
  methods: {
    async getList() {
      let res = await getCatelistTree();
      this.cateList = res.list;
    },
    limitImg() {
      this.$message.warning("分类图片最多添加1个！");
    },
    // 设置当前组件状态
    setStatus(val) {
      // this.dialogVisible = val.isShow;
      // this.isAdd = val.isAdd
      this.ruleForm = { ...val };
      // 二级分类有图片，需要显示出来
      if (this.ruleForm.img) {
        this.fileList.push({
          name: "小U商城",
          url: this.$host + this.ruleForm.img,
        });
      }
    },
    // 提交表单
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 如果是一级分类就会有children 需要删除
          this.ruleForm.children ? delete this.ruleForm.children : "";
          // 创建FormDate对象
          let fd = new FormData();
          // 给FormDate赋值的方式： fd.append('name',jack)
          for (var key in this.ruleForm) {
            // 遍历对象，将this.ruleForm的值传给fd
            fd.append(key, this.ruleForm[key]);
          }
          console.log(fd);
          //
          fd.forEach((key, val) => {
            console.log(key, val);
          });

          // 封装请求
          let res;
          if (this.info.isAdd) {
            res = cateAdd(fd);
            this.$bus.$emit("update");
          } else {
            res = cateEdit(fd);
          }
          if (res) {
            // 重置弹窗数据
            this.cancel();
            // 关闭弹窗
            this.info.isShow = false;
            // 兄弟组件传值 告诉list组件更新数据
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.ruleForm = { ...defaultData };
      // 关闭弹窗
      this.dialogVisible = false;
      // 关闭弹窗时 清理文件列表
      this.fileList = [];
      // 关闭弹窗
      this.info.isShow = false;
    },
    // 修改文件时触发
    handleChange(file, fileList) {
      // 给img赋值，传给fd
      this.ruleForm.img = fileList[0].raw;
    },
    // 删除图片的监听
    handleRemove() {
      this.ruleForm.img = "";
    },
    handlePictureCardPreview() {
      this.imageUrl = this.fileList[0].url;
      this.dialogVisible = true;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.cell img {
  width: 160px;
  height: 70px;
}
</style>